<?php
include_once("$config[webroot]/lib/Httpsqs.php");
include_once("../module/product/includes/plugin_order_class.php");
include("../lang/cn/company_type_config.php");
$order = new order;

if($_POST['act'] && $_POST['act']=='product_opear'){//设置供应商
    if($_POST['id'] && $_POST['id']>0){
        $set = "";
        switch($_POST['type']){
            case 1:
                $set = " ms_stock_supplierid={$_POST['supplierId']}";
                break;
            case 2:
                $set = " ms_entrepot_supplierid={$_POST['supplierId']}";
                break;
            case 3:
                $set = " ms_direct_supplierid={$_POST['supplierId']}";
                break;
            default:
                break;
        }
        if(!empty($set)){
            $sql = "update ".SETMEAL." set {$set} where id={$_POST['id']}";
            $db->query($sql);
            
            //更新solr
            $sql = "select pid from ".SETMEAL." where id={$_POST['id']}";
            $db->query($sql);
            $pid = $db->fetchField('pid');
            if($pid>0){
                Httpsqs::put('solrProducts',array('product_id'=>$pid));
            }
        }
    }
    
    die(json_encode(array('code'=>0,'msg'=>'success')));
}

if($_POST['act'] && $_POST['act']=='setMsStatus'){//sku上下架
    if($_POST['id'] && $_POST['id']>0){
        $set = "";
        if($_POST['ms_status']!=''){
            $set .= " ms_status={$_POST['ms_status']}";
        }
        if(!empty($set)){
            $sql = "update ".SETMEAL." set {$set} where id={$_POST['id']}";
            $db->query($sql);
            
            $sql = "select pid from ".SETMEAL." where id={$_POST['id']}";
            $db->query($sql);
            $pid = $db->fetchField('pid');
            
            //如果是下架，则检查是否所有SKU已下架
            if($_POST['ms_status']=='-3'){
                $sql = "select id from ".SETMEAL." where pid={$pid} and ms_status>0 limit 1";
                $db->query($sql);
                $onProId = $db->fetchRow();
                if(empty($onProId)){//无上架商品则下架主商品
                    $sql = "update ".PRO." set ms_status=-3 where id={$pid}";
                    $db->query($sql);
                }
            }else{
                $sql = "update ".PRO." set ms_status=1 where id={$pid}";
                $db->query($sql);
            }
            
            //更新主商品价格
            $sql = "update ".PRO." a set a.ms_price=(select min(ms_price) from `haiou_product_sku` b where b.`pid`={$pid} and b.`ms_status`>-3) where a.id={$pid}";
            $db->query($sql);
            
            //更新solr
            if($pid>0){
                Httpsqs::put('solrProducts',array('product_id'=>$pid));
            }
        }
    }

    die(json_encode(array('code'=>0,'msg'=>'success')));
}

if($_GET['act']=='product_opear'){
    $skuInfo['id'] = $_GET['skuId'];
    $skuInfo['type'] = $_GET['type'];
    $skuInfo['supplierId'] = $_GET['supplierId'];
    //供应商列表
    $sql = "select a.supplieid,a.stock,b.title from haiou_stock_supplie_products a left join haiou_stock_suppliers b on a.supplieid=b.id left join haiou_stock_shops c on a.shopid=c.id where a.product_sku_id={$_GET['skuId']} and a.status>0 and c.type={$_GET['type']}";
    $db->query($sql);
    $suppliers = $db->getRows();
    
    $tpl->assign('suppliers',$suppliers);
    $tpl->assign('skuInfo',$skuInfo);
    $tpl->display('product_opear.htm');
    exit;
}

if($_POST['act'] && $_POST['act']=='savePrice'){//保存微店价格
    if($_POST['id'] && $_POST['id']>0){
        if($_POST['ms_price'] && $_POST['ms_price']>0){
            $sql = "update ".SETMEAL." set ms_price='{$_POST['ms_price']}' where id={$_POST['id']}";
            $db->query($sql);
            
            //更新solr
            $sql = "select pid from ".SETMEAL." where id={$_POST['id']}";
            $db->query($sql);
            $pid = $db->fetchField('pid');
            if($pid>0){
                Httpsqs::put('solrProducts',array('product_id'=>$pid));
            }
        }
    }
    die();
}

if($_POST['act'] && $_POST['act']=='saveDiscount'){//保存微店返利调价字段
    if($_POST['id'] && $_POST['id']>0){
        $sql = "update ".SETMEAL." set ms_discount='{$_POST['ms_discount']}' where id={$_POST['id']}";
        $db->query($sql);

        //更新solr
        $sql = "select pid from ".SETMEAL." where id={$_POST['id']}";
        $db->query($sql);
        $pid = $db->fetchField('pid');
        if($pid>0){
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
    }
    die();
}

if($_POST['act'] && $_POST['act']=='saveBuyLimit'){//保存微店限购字段
    if($_POST['id'] && $_POST['id']>0){
        $sql = "update ".SETMEAL." set ms_buy_limit='{$_POST['ms_buy_limit']}' where id={$_POST['id']}";
        $db->query($sql);

        //更新solr
        $sql = "select pid from ".SETMEAL." where id={$_POST['id']}";
        $db->query($sql);
        $pid = $db->fetchField('pid');
        if($pid>0){
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
    }
    die();
}

if($_POST['act'] && $_POST['act']=='savetaxation'){//保存微店税费
    if($_POST['id'] && $_POST['id']>0){
        $sql = "update ".SETMEAL." set ms_taxation='{$_POST['ms_taxation']}' where id={$_POST['id']}";
        $db->query($sql);

        //更新solr
        $sql = "select pid from ".SETMEAL." where id={$_POST['id']}";
        $db->query($sql);
        $pid = $db->fetchField('pid');
        if($pid>0){
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
    }
    die();
}



if($_POST['act'] && $_POST['act']=='add'){//保存添加商品
    if(!empty($_POST['chk']))
    {
        $productIdStr = '';
        $productSkuIdStr = '';
        foreach($_POST['chk'] as $val)
        {
            if(!empty($val)){
                $productIdStr .= empty($productIdStr)?$val:','.$val;
                $sql = "select id from ".SETMEAL." where pid={$val} ";
                $db->query($sql);
                $sku_list = $db->getRows();
                if(!empty($sku_list)){
                    foreach ($sku_list as $skuId){
                        $productSkuIdStr .= empty($productSkuIdStr)?$skuId['id']:','.$skuId['id'];
                    }
                }
                //更新solr
                Httpsqs::put('solrProducts',array('product_id'=>$val));
            }
        }

        $productIdStr = trim($productIdStr);
        $productSkuIdStr = trim($productSkuIdStr);
        
        $nowTime = date('Y-m-d H:i:s');

        $sql = "update ".PRO." set ms_status=status,ms_show_time='{$nowTime}' where id in({$productIdStr})";
        $db->query($sql);

        $sql = "update ".SETMEAL." set ms_status=status,ms_show_time='{$nowTime}',ms_price=price,ms_buy_limit=buy_limit where id in({$productSkuIdStr})";
        $db->query($sql);
    }
    echo "<script>window.parent.main.location.reload();</script>";
    echo "<script>window.parent.DialogManager.close('activity');</script>";
    die;
}

if($_GET['type'] && $_GET['type']=='addPro'){//添加商品的弹出层
    $id=$_GET['id']?$_GET['id']:NULL;
    $product_id=$_GET['product_id']?$_GET['product_id']:NULL;
    $key=$_GET['key']?$_GET['key']:NULL;
    $catid=$_GET['catid']?$_GET['catid']:NULL;
    $code=$_GET['code']?$_GET['code']:NULL;
    $tags=$_GET['tags']?$_GET['tags']:NULL;

    if(!empty($key))
        $scl.=" and ( pname like '%$key%' )";
    if(!empty($catid))
        $scl.=" and LOCATE($catid,catid)=1";
    if(!empty($tags))
        $scl.=" and (tags like '$tags%' or tags like '%,$tags%') ";
    if(!empty($code))
        $scl.=" and code = '$code'";
    if(!empty($product_id)){
        $scl.=" and id in($product_id)";
        $_GET['page_size'] = substr_count($product_id,',')+1;
    }

    if($_GET['brand'] && $_GET['brandid']){
        $scl.=" and brandid = '$_GET[brandid]'";
    }else{
        $scl.=" and brand like '".addslashes($_GET[brand])."%'";
    }

    if ($_GET['page_size']){
        $page_size=$_GET['page_size'];
    }else{
        $_GET['page_size']=$page_size=20;
    }

    $sql="SELECT id,catid,tags,brand,pic,pname,price,show_price,market_price,code,tax_rate FROM ".PRO." where status>-9 and ms_status=0 $scl order by id desc";
    //====================
    include_once("../includes/page_utf_class.php");
    $page = new Page;
    $page->listRows=$page_size;
    if (!$page->__get('totalRows')){
        $db->query($sql);
        $totalRows = $page->totalRows = $db->num_rows();
    }else{
        $totalRows = $_GET['totalRows'];
    }
    $sql .= "  limit ".$page->firstRow.",".$page->listRows;
    $de['page'] = $page->prompt();
    //=====================
    $db->query($sql);
    $de['list']=$db->getRows();
    //$tpl->assign("de",$de);

    if(!empty($catid) and $de['list'])
    {
        $max=$catid.'99';
        $min=$catid.'00';
        $ss=" and catid<=$max and catid>=$min ";
    }
    elseif(!empty($catid) and !$de['list'])
    {
        $catid=substr($catid,0,-2);
        $max=$catid.'99';
        $min=$catid.'00';
        $ss=" and catid<=$max and catid>=$min ";
    }
    else
        $ss=" and catid<9999 ";

    $sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
    $db->query($sql);
    $re=$db->getRows();
    if(!$re)
    {
        $catid=substr($catid,0,-2);
        $max=$catid.'99';
        $min=$catid.'00';
        $ss=" and catid<=$max and catid>=$min ";
        $sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
        $db->query($sql);
        $re=$db->getRows();
    }
    $tpl->assign("re",$re);

    include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
    //================================================================
    $pro=new pro();
    $de['all_tags']=$pro->get_tags();

    if ($_GET['tags']){

        $de['tags']=array($_GET['tags']);


    }

    //所属分类，产品标签
    $catids = "";
    $tagids = "";
    $tagidsArr = array();
    $catTmp = array();
    $tagTmp = array();
    foreach ($de['list'] as $dk=>$dv)
    {
        $catids .= $dv['catid'].",";
//         $tagids .= $dv['tags'].",";
        if(!empty($dv['tags'])){
            $tagidsArr[] = $dv['tags'];
        }
    }
    $catids = trim($catids,",");
    if($catids!=""){
        $sql="SELECT cat,catid FROM ".PCAT." WHERE catid IN (".$catids.")";
        $db->query($sql);
        $catTmp=$db->getRows();
    }
    $catArr = array();
    foreach ($catTmp as $cav){
        $catArr[$cav['catid']] = $cav['cat'];
    }
    
    $tagidsArr = array_unique($tagidsArr);
    $tagids = implode(',', $tagidsArr);
    if($tagids!=""){
        $sql="SELECT id,tags FROM ".TAG." WHERE id IN (".$tagids.")";
        $db->query($sql);
        $tagTmp=$db->getRows();
    }
    $tagArr = array();
    foreach ($tagTmp as $tav)
    {
        $tagArr[$tav['id']] = $tav['tags'];
    }


    foreach ($de['list'] as $dk=>$dv)
    {
        $de['list'][$dk]['catname'] = $catArr[$dv['catid']];
        $tagsTmp = explode(",", $dv['tags']);
        $tagStr = "";
        foreach ($tagsTmp as $ttv)
        {
            $tagStr .= $tagArr[$ttv].",";
        }
        $de['list'][$dk]['tagnames'] = rtrim($tagStr,",");
    }

    $tpl->assign("de",$de);
    $tpl->assign("countpro",$totalRows);
    $condTemp = $_GET;
    unset($condTemp['m']);
    unset($condTemp['s']);
    $cond = "";
    if(count($condTemp)){
        $cond = "&".http_build_query($condTemp);
    }
    $tpl->assign("cond",$cond);
    $tpl->assign("config",$config);
    $tpl->display("activity_product.htm");
    exit;
}


if($_GET['operation']=='getbrand'){
    //根据品牌名搜索品牌
    function get_brand_new($brandName,$limit=10){
        global $db;
        if($brandName==''){
            $db->query("SELECT id,name FROM ".BRAND." ORDER BY create_time DESC LIMIT ".$limit);
        }else{
            $db->query("SELECT id,name FROM ".BRAND." WHERE name LIKE '%".mysql_real_escape_string($brandName)."%' ORDER BY create_time DESC");
        }
        $brandInfo = $db->getRows();
        $op = "";
        foreach($brandInfo as $v)
        {
            $op.='<li style="cursor:pointer;" bid="'.$v["id"].'">'.$v["name"].'</li>';
        }
        return $op;
    }
    echo get_brand_new($_GET['brandname']);
    exit;
}
if($_GET['act']=="export_product_id"||$_GET['act']=='export_product'||$_GET['act']=='export_product_sku'){
    $act = $_GET['act'];
    $_GET = $_GET['param'];
    $_GET['act']=$act;unset($act);
}
if($_POST['act']=='op')
{//商品状态
     
    if(!in_array($_POST['type'], array(-9,-1,1,2,-3))){
        exit('type -9,-1,1,2,-3 error');
    }
     
     
    if(is_array($_POST['chk']))
    {
        $id=implode(",",$_POST['chk']);
        
        $nowTime = date('Y-m-d H:i:s');
        
        $sql="update ".PRO." set ms_status=".$_POST['type'].",ms_show_time='{$nowTime}' where id in ($id)";
        $db->query($sql);
        $sql="update ".SETMEAL." set ms_status=".$_POST['type'].",ms_show_time='{$nowTime}' where pid in ($id) and `status`>-9";
        	
        $db->query($sql);
        	
        //更新solr
        $idArr = explode(',', $id);
        foreach ($idArr as $pid)
        {
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
        	
        unset($_GET['m']);
        unset($_GET['s']);
        $param = "";
        if(count($_GET)){
            $param .= "&".http_build_query($_GET);
        }
        msg("?m=micro_shop&s=products_list.php".$param);
    }
    
    exit;
}

if($_POST['act']=='op_express')
{//发货方式
     
    if(!in_array($_POST['type'], array(1,2,3))){
        exit('type 1,2,3 error');
    }
     
     
    if(is_array($_POST['chk']))
    {
        $str = "";
        if($_POST['type']!=3){
            $str = ",ms_only_sale_stock=0";
        }
        
        $id=implode(",",$_POST['chk']);
        $sql="update ".PRO." set ms_express_type=".$_POST['type'].$str." where id in ($id)";
        $db->query($sql);
        
        $sql="update ".SETMEAL." set ms_express_type=".$_POST['type'].$str." where pid in ($id) and `status`>-9";
        $db->query($sql);
         
        //更新solr
        $idArr = explode(',', $id);
        foreach ($idArr as $pid)
        {
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
         
        unset($_GET['m']);
        unset($_GET['s']);
        $param = "";
        if(count($_GET)){
            $param .= "&".http_build_query($_GET);
        }
        msg("?m=micro_shop&s=products_list.php".$param);
    }

    exit;
}

if($_POST['act']=='op_only_sale_stock')
{//只卖成都仓库存
     
    if(!in_array($_POST['type'], array(0,1))){
        exit('type 0,1 error');
    }
     
     
    if(is_array($_POST['chk']))
    {
        $id=implode(",",$_POST['chk']);
        $sql="update ".PRO." set ms_only_sale_stock=".$_POST['type']." where id in ($id) and ms_express_type in(1,3)";
        $db->query($sql);

        $sql="update ".SETMEAL." set ms_only_sale_stock=".$_POST['type']." where pid in ($id) and `status`>-9 and ms_express_type in(1,3)";
        $db->query($sql);
         
        //更新solr
        $idArr = explode(',', $id);
        foreach ($idArr as $pid)
        {
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
         
        unset($_GET['m']);
        unset($_GET['s']);
        $param = "";
        if(count($_GET)){
            $param .= "&".http_build_query($_GET);
        }
        msg("?m=micro_shop&s=products_list.php".$param);
    }

    exit;
}

if($_POST['act']=='op_ms_has_material')
{//有无推广素材
 
    if(!in_array($_POST['type'], array(0,1))){
        exit('type 0,1 error');
    }
     
     
    if(is_array($_POST['chk']))
    {
        $id=implode(",",$_POST['chk']);
        $sql="update ".PRO." set ms_has_material=".$_POST['type']." where id in ($id)";
        $db->query($sql);
         
        //更新solr
        $idArr = explode(',', $id);
        foreach ($idArr as $pid)
        {
            Httpsqs::put('solrProducts',array('product_id'=>$pid));
        }
         
        unset($_GET['m']);
        unset($_GET['s']);
        $param = "";
        if(count($_GET)){
            $param .= "&".http_build_query($_GET);
        }
        msg("?m=micro_shop&s=products_list.php".$param);
    }
    
    exit;
}

//----------------------分仓
$general_shop =$order->get_general_shop();

if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
    msg('noright.php');
    exit;
}
if ($general_shop){
    $_GET['stock_shop'] =$general_shop;
}

$psql='';
if($_GET['name'])
{
    $psql.=" and a.pname like '%$_GET[name]%' ";
}
if($_GET['code'])
{
    $psql.=" and a.id in (".trim($_GET[code],',').")";
}
if($_GET['statu']!=='' && isset($_GET['statu'])){
    $psql.=" and a.ms_status = '$_GET[statu]'";

}
if($_GET['cat'])
{
    $psql.=" and a.catid like '$_GET[cat]%'";
}
if($_GET['brand'] && $_GET['brandid']){
    $psql.=" and a.brandid = '$_GET[brandid]'";
}else{
    $psql.=" and a.brand like '$_GET[brand]%'";
}
if(!empty($_GET['tags']))
{
    $psql.=" and (a.tags like '$_GET[tags]%' or a.tags like '%,$_GET[tags]%') ";
}
if(!empty($_GET['stock_shop']))
{
    $psql.=" and a.stock_shop='$_GET[stock_shop]'";
}
if($_GET['ms_express_type']){
    $psql.=" and a.ms_express_type='{$_GET['ms_express_type']}'";
}
if(isset($_GET['ms_only_sale_stock']) && $_GET['ms_only_sale_stock']!=''){
    $psql.=" and a.ms_only_sale_stock='{$_GET['ms_only_sale_stock']}'";
}
if(isset($_GET['ms_has_material']) && $_GET['ms_has_material']!=''){
    $psql.=" and a.ms_has_material='{$_GET['ms_has_material']}'";
}
if(isset($_GET['ms_stock_supplierid']) && $_GET['ms_stock_supplierid']!=''){
    $psql.=" and b.ms_stock_supplierid='{$_GET['ms_stock_supplierid']}'";
}
if(isset($_GET['ms_entrepot_supplierid']) && $_GET['ms_entrepot_supplierid']!=''){
    $psql.=" and b.ms_entrepot_supplierid='{$_GET['ms_entrepot_supplierid']}'";
}
if(isset($_GET['ms_direct_supplierid']) && $_GET['ms_direct_supplierid']!=''){
    $psql.=" and b.ms_direct_supplierid='{$_GET['ms_direct_supplierid']}'";
}

if (!isset($_GET['admin_user'])){
    $db->query(" select 1 from ".ADMIN." where group_id>0 and user='$_SESSION[ADMIN_USER]'");
    if($db->num_rows()){
        $admin_user = $_SESSION[ADMIN_USER];
    }
}elseif (!empty($_GET['admin_user'])){
    $admin_user=$_GET['admin_user'];
    $psql.=" and a.create_user = '$admin_user'";
}

if ($admin_user){
    $tpl->assign('admin_user',$admin_user);
}



$export_sql=$sql="SELECT GROUP_CONCAT(b.code) as setmeal_code,
	GROUP_CONCAT(b.stock) as setmeal_stock,
	GROUP_CONCAT(b.ms_price) as setmeal_price,
	GROUP_CONCAT(b.ms_discount) as setmeal_ms_discount,
	GROUP_CONCAT(b.ms_status) as setmeal_ms_status,
	GROUP_CONCAT(b.market_price) as setmeal_market_price,
	GROUP_CONCAT(b.id) as setmeal_id,
	GROUP_CONCAT(b.spec) as setmeal_spec,
	GROUP_CONCAT(b.ms_buy_limit) as setmeal_ms_buy_limit,
    GROUP_CONCAT(b.ms_taxation) as setmeal_ms_taxation,
	GROUP_CONCAT(b.ms_stock_supplierid) as setmeal_ms_stock_supplierid,
	GROUP_CONCAT(b.ms_entrepot_supplierid) as setmeal_ms_entrepot_supplierid,
	GROUP_CONCAT(b.ms_direct_supplierid) as setmeal_ms_direct_supplierid,

	count(b.id) as setmeal_nums,

	b.id,
	b.pid,
	a.pic,
	a.pname,
	a.brand,
	a.catid,
	a.tags,
	a.all_stock,
	a.code,
	a.waiting_time,
	a.ms_show_time,
	a.waiting_time_end,
	a.stock_shop,
    a.ms_express_type,
    a.ms_only_sale_stock,
    a.ms_has_material,
	b.uptime,
	FROM_UNIXTIME(b.uptime, '%Y-%m-%d %H:%i:%s' )as buptime,
	a.ms_status,
    a.create_user,
    a.create_time
	FROM ".PRO." a left join ".SETMEAL." b on a.id = b.pid WHERE a.ms_status!=0 and b.ms_status!=0 and a.ms_status>-9 and b.ms_status>-9 $psql group by pid order by a.ms_show_time desc";
//====================
include_once("../includes/page_utf_class.php");//echo $sql."<br/>";
$page = new Page;
$page->listRows=20;
$db->query($sql);
$totalRows = $db->num_rows();
if (!$page->__get('totalRows')){
    $db->query($sql);
    $page->totalRows = $db->num_rows();
}
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=====================

if($_GET['act']=="export_product_id"||$_GET['act']=='export_product'||$_GET['act']=='export_product_sku'){
    include_once '../lib/php_excel.php';
    $db->query($export_sql);
    $export_data=$db->getRows();
    if ($_GET['act']=='export_product'){
        //商品ID	skuid	SKUCODE	商品条码	1级分类	2级分类	3级分类	品牌	商品名称	商品短描述	销售属性	销售价	成本价	市场价	重量	备货期	实际库存	平均采购价格
         
        $queue[0] = array("商品id","SKU总数","1级分类","2级分类","3级分类","商品名称","品牌","微店价","市场价","备货期","总库存","发货方式","状态","作者","创建时间","最后修改时间");//定义表头
    }elseif ($_GET['act']=='export_product_sku'){
        $queue[0] = array("商品id","SKU_ID","商品名称","规格","售价","状态");//定义表头
        	
    }else {
        $queue[0] = array("商品id");//定义表头
    }
    $arr = array();
     
    $status_array=array('-3'=>'已下架','-2'=>'已送审','-1'=>'被驳回','0'=>'未送审','1'=>'已上架','2'=>'推荐');
    foreach ($export_data as $key=>$val)
    {
        foreach (array('code','stock','price','market_price','ms_status','ms_discount','ms_buy_limit','id','spec','ms_taxation') as $item){
        
            $_tmp_flag= explode(',', $val['setmeal_'.$item]);
        
            $export_data['list'][$key]['setmeal_'.$item]=$_tmp_flag;
            $val['setmeal_'.$item]=$_tmp_flag;
        
        }
        if ($_GET['act']=="export_product_id"){
            $arr[] = array($val['pid']);
        }
        elseif ($_GET['act']=='export_product'){
            $catname = array();
            
            if(strlen($val['catid'])>8)
                $catname[]=getName(substr($val['catid'],0,-6));
            if(strlen($val['catid'])>6)
                $catname[]=getName(substr($val['catid'],0,-4));
            if(strlen($val['catid'])>4)
                $catname[]=getName(substr($val['catid'],0,-2));
                         
            $catname[]=getName($val['catid']);
            
            if($val['waiting_time'] && $val['waiting_time_end']){
                $val['waiting_time_desc'] = $val['waiting_time']."-".$val['waiting_time_end']."天发货";
            }elseif($val['waiting_time']){
                $val['waiting_time_desc'] = $val['waiting_time']."天发货";
            }elseif($val['waiting_time_end']){
                $val['waiting_time_desc'] = $val['waiting_time_end']."天发货";
            }else{
                $val['waiting_time_desc'] = "暂无";
            }
                         
            $expressName = "";
            switch ($val['ms_express_type'])
            {
                case 1:
                    $expressName = "不限";
                    break;
                case 2:
                    $expressName = "直邮";
                    break;
                case 3:
                    $expressName = "拼邮";
                    break;
                default:
                    break;
            }
                         
            $arr[] = array($val['pid'],$val['setmeal_nums'],$catname[0],$catname[1],$catname[2],$val['pname'],$val['brand'],$val['setmeal_price'],$val['setmeal_market_price'],$val['waiting_time_desc'],$val['all_stock'],$expressName,$status_array[$val['ms_status']],$val['create_user'],$val['create_time'],$val['buptime']);
        }elseif ($_GET['act']=='export_product_sku'){
            foreach ($val['setmeal_id'] as $k=>$skuid)
            {
                $arr[] = array($val['pid'],$skuid,$val['pname'],$val['setmeal_spec'][$k],$val['setmeal_price'][$k],$productStatus[$val['setmeal_ms_status'][$k]]);
            }
        }
    }
    $export_data = array_merge($queue,$arr);
    $filename =   "商品信息" . date ( "YmdHis" );
    $xls = new Excel_XML ( 'UTF-8', false, '商品信息' );
    $xls->addArray ( $export_data );
    $xls->generateXML ( $filename );
    exit ();
}else{
    $db->query($sql);//echo $sql;exit;
    $de['list']=$db->getRows();
}

foreach($de['list'] as $key=>$val)
{
    $catname=array();
    if(strlen($val['catid'])>8)
        $catname[]=getName(substr($val['catid'],0,-6));
        if(strlen($val['catid'])>6)
            $catname[]=getName(substr($val['catid'],0,-4));
            if(strlen($val['catid'])>4)
                $catname[]=getName(substr($val['catid'],0,-2));

                $catname[]=getName($val['catid']);

                $de['list'][$key]['catname']=implode('->',$catname);
                $de['list'][$key]['tags_arr'] = get_product_tags($val['tags']);

                if($val['waiting_time'] && $val['waiting_time_end']){
                    $de['list'][$key]['waiting_time_desc'] = $val['waiting_time']."-".$val['waiting_time_end']."天发货";
                }elseif($val['waiting_time']){
                    $de['list'][$key]['waiting_time_desc'] = $val['waiting_time']."天内发货";
                }elseif($val['waiting_time_end']){
                    $de['list'][$key]['waiting_time_desc'] = $val['waiting_time_end']."天内发货";
                }else{
                    $de['list'][$key]['waiting_time_desc'] = "暂无";
                }

                //处理 多规格的情况 zx2014新增


                foreach (array('code','stock','price','market_price','ms_status','ms_discount','ms_buy_limit','ms_taxation','ms_stock_supplierid','ms_entrepot_supplierid','ms_direct_supplierid','id','spec') as $item){

                    $_tmp_flag= explode(',', $val['setmeal_'.$item]);

                    $de['list'][$key]['setmeal_'.$item]=$_tmp_flag;

                }
}



//分类

$sql="select * from ".PCAT." where catid<9999 order by displayorder,catid";
$db->query($sql);
$de['cat']=$db->getRows();
foreach($de['cat'] as $key=>$val)
{
    $sql="select * from ".PCAT." where catid < '".$val['catid']."99' and catid > '".$val['catid']."00' order by displayorder,catid";
    $db->query($sql);
    $a=$db->getRows();
    foreach($a as $ke=>$va)
    {
        $sql="select * from ".PCAT." where catid < '".$va['catid']."99' and catid > '".$va['catid']."00' order by displayorder,catid";
        $db->query($sql);
        $a[$ke]['scat']=$db->getRows();
    }
    $de['cat'][$key]['scat']=$a;
}

$sql=" select user,name from ".ADMIN." where group_id>57";
$db->query($sql);
$de['admins']=$db->getRows();



unset($_GET['s']);
unset($_GET['m']);
unset($_GET['category_id']);
unset($_GET['catid']);
unset($_GET['tcatid']);
unset($_GET['scatid']);
unset($_GET['sscatid']);

//所有标签
include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
//================================================================
$pro=new pro();
$de['all_tags']=$pro->get_tags();
if ($_GET['tags']){
    $de['tags']=array($_GET['tags']);
}

$de["stock_shops"]=$pro->get_stock_shops();

if (count($_GET)){
    $tpl->assign("url",'&'.implode('&',convert($_GET)));
}

//仓库
$stock_shops=$order->get_stock_shops();
$tpl->assign("stock_shops",$stock_shops);

//供应商搜索条件
$suppliersList = [];
$sql = "SELECT DISTINCT(a.ms_stock_supplierid) as id,b.title FROM `haiou_product_sku` a LEFT JOIN haiou_stock_suppliers b ON a.ms_stock_supplierid=b.id where ms_status>0 and ms_stock_supplierid>0";//现货
$db->query($sql);
$suppliersList['stock'] = $db->getRows();
$sql = "SELECT DISTINCT(a.ms_entrepot_supplierid) as id,b.title FROM `haiou_product_sku` a LEFT JOIN haiou_stock_suppliers b ON a.ms_entrepot_supplierid=b.id where ms_status>0 and ms_entrepot_supplierid>0";//保税仓
$db->query($sql);
$suppliersList['entrepot'] = $db->getRows();
$sql = "SELECT DISTINCT(a.ms_direct_supplierid) as id,b.title FROM `haiou_product_sku` a LEFT JOIN haiou_stock_suppliers b ON a.ms_direct_supplierid=b.id where ms_status>0 and ms_direct_supplierid>0";//直邮
$db->query($sql);
$suppliersList['direct'] = $db->getRows();

$tpl->assign("de",$de);
$tpl->assign("suppliersList",$suppliersList);
$tpl->assign("countpro",$totalRows);
$tpl->assign("config",$config);
$tpl->display('products_list.htm');